package com.lbb.cas.atomic;

import java.util.concurrent.atomic.AtomicReference;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class AtomicStock {
	
	Logger log = LoggerFactory.getLogger(AtomicStock.class);
	private AtomicStock(){}
	
	private static final AtomicStock stock = new AtomicStock();
//	private volatile int count = 50;
	
	
	AtomicReference<Integer> ar = new AtomicReference<Integer>(50);
	
	
	public static AtomicStock getStock(){
		return stock;
	}
	
	public void stock(){
		
		
		
//		log.debug("线程 {},库存:{}。。。。。。。。。",Thread.currentThread().getName(),ar.get());
		
		if (ar.get() >0){
			
			
			// 当前值      期望值
			boolean result  = ar.compareAndSet(ar.get(), ar.get()-1);
			
			if (result){
				
				
				log.debug("线程 {} ,购买成功",Thread.currentThread().getName(),ar.get());
			}
			else {
				
				
				log.debug("线程 {} ,购买失败",Thread.currentThread().getName());
			}
			
			log.debug("线程 {} ,库存扣减:{}.....",Thread.currentThread().getName(),ar.get());
		}
		
		else {
			log.debug("线程 {}卖光了................",Thread.currentThread().getName());
			
		}
		
		
	}

}
